package com.itheima.service.mongo.impl;

import cn.hutool.core.collection.CollUtil;
import com.itheima.domain.mongo.RecommendUser;
import com.itheima.domain.mongo.UserLike;
import com.itheima.service.mongo.RecommendUserService;
import com.itheima.vo.PageBeanVo;
import org.apache.dubbo.config.annotation.DubboService;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;

@DubboService
public class RecommendUserServiceImpl implements RecommendUserService {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public RecommendUser findTodayBest(Long toUserId) {
        // 1.构建条件
        Query query = new Query(
                Criteria.where("toUserId").is(toUserId)
        ).with(Sort.by(Sort.Order.desc("score")))
                .skip(0).limit(1);
        // 2.查询推荐用户
        RecommendUser recommendUser = mongoTemplate.findOne(query, RecommendUser.class);
        // 3.返回一条
        return recommendUser;
    }

    @Override
    public PageBeanVo findByPage(Long toUserId, Integer pageNum, Integer pageSize) {
        //1.创建条件对象(用户id)
        Query query = new Query(Criteria.where("toUserId").is(toUserId));

        //2.查询推荐用户表获取总条数
        long count = mongoTemplate.count(query, RecommendUser.class);

        //3.给条件对象追加参数(分页和排序)
        query.with(Sort.by(Sort.Order.desc("score")))
                .skip((pageNum - 1) * pageSize + 1).limit(pageSize);

        //4.查询当前页的数据集合
        List<RecommendUser> list = mongoTemplate.find(query, RecommendUser.class);

        /*
        推荐的用户只有1个
        总记录数就是1,但是查询list大小为0
        最好封装pageBeanVo的时候,对count进行重新计算
         */
        if (CollUtil.isNotEmpty(list) && list.size() == 0) {
            count = 0;
        } else if (list.size() > 1) {
            count = count - 1;
        }

        //5.封装pageBeanVo且返回
        return new PageBeanVo(pageNum, pageSize, count, list);
    }

    // 查询二个的缘分值
    @Override
    public RecommendUser findPersonalInfo(Long toUserId, Long recommendUserId) {
        //1.创建条件对象(俩id)
        Query query = new Query(Criteria.where("toUserId").is(toUserId).and("userId").is(recommendUserId));
        //2.查询推荐表,返回推荐用户
        return mongoTemplate.findOne(query, RecommendUser.class);
    }

    //卡片
    @Override
    public List<RecommendUser> findCards(Long toUserId) {
        // 1.构建条件
        Query query = new Query(Criteria.where("toUserId").is(toUserId));
        // 2.查询推荐用户
        List<RecommendUser> recommendUserList = mongoTemplate.find(query, RecommendUser.class);
        // 3.返回一条
        return recommendUserList;

    }

    //不喜欢表
    @Override
    public void RecommendUserUnLike(Long unLikeUserId) {
        // 1.构建条件
        Query query = new Query(Criteria.where("toUserId").is(unLikeUserId));
        mongoTemplate.remove(query, RecommendUser.class);

    }

    //喜欢表
    @Override
    public UserLike RecommendUserLike(Long userId, Long likeUserId) {
        // 1.构建条件

        Query query = new Query(Criteria.where("likeUserId").is(likeUserId).and("userId").is(userId));
        // 2.查询用户

        long count = mongoTemplate.count(query, UserLike.class);
        if (count>0){
            return null;
        }
        //封装
        UserLike like = new UserLike();
        like.setId(ObjectId.get());
        like.setUserId(userId);
        like.setLikeUserId(likeUserId);
        like.setCreated(System.currentTimeMillis());
        UserLike userLike = mongoTemplate.save(like);
        return userLike;
    }
    //判断是否是互相喜欢
    @Override
    public boolean UserLike(Long userId, Long likeUserId) {
        Criteria criteria1 = Criteria.where("userId").is(userId).and("likeUserId").is(likeUserId);
        Criteria criteria2 = Criteria.where("userId").is(likeUserId).and("likeUserId").is(userId);
        Criteria criteria = new Criteria().orOperator(criteria1, criteria2);
        Query query1 = new Query(criteria);
        long count1 = mongoTemplate.count(query1, UserLike.class);
        return count1==2;
    }

}